package dailyExe.shuzu;

public class 删除有序数组中的重复项II {
    public static void main(String[] args) {
        removeDuplicates(new  int[]{1,1,1,2,2,3});
    }

    /**
     * 解决方案一 使用双指针
     * @param nums
     * @return
     */
    public static int removeDuplicates(int[] nums) {
        if (nums.length <= 2) return nums.length;
        //看前两个是否一样
        // 使用快慢指针的方式解决
        int index = 2;
        int ans = 2;
        int count = nums[0] == nums[1] ? 2:1;
        for (int i = 2; i < nums.length; i++) {
            if (nums[i - 1] == nums[i]) {
                count++;
                if (count <= 2) {
                    ans++;
                    nums[index] = nums[i];
                    index++;
                }
            }else {
                ans++;
                count = 1;
                nums[index] = nums[i];
                index++;
            }
        }

        return  ans;
    }


}
